package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.CustomerInfoService;
import com.igeekhome.biz.NoticeService;
import com.igeekhome.pojo.CustomerInfo;
import com.igeekhome.pojo.CustomerService;
import com.igeekhome.pojo.Notice;
import com.igeekhome.pojo.WorkOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 客户信息 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/customerInfo")
public class CustomerInfoController {
    @Autowired
    private CustomerInfoController customerInfoController;
    @Autowired
    private CustomerInfoService customerInfoService;

    @RequestMapping("/index")
    public String index(Model model, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
        initcustomerInfoService(model,current,size);
        return "customerinfo";
    }

    public void initcustomerInfoService(Model model, Integer current, Integer size)
    {

        IPage<CustomerInfo> page=new Page<>(current,size);
        IPage<CustomerInfo> page1 = this.customerInfoService.page(page);

        List<CustomerInfo> list = page1.getRecords();
        long pagesCount = page1.getPages();


        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);
        model.addAttribute("main","客户管理页面");
    }
    @RequestMapping("/update")
    public String update(Model model, CustomerInfo customerInfo,@RequestParam("current") Integer current, @RequestParam("size") Integer size)
    {
        // 根据id将对像查询
        customerInfo= this.customerInfoService.getById(customerInfo.getId());
        model.addAttribute("cs",customerInfo);
        model.addAttribute("main","客户修改");
        model.addAttribute("current",current);
        return  "updatecustomerinfo";
    }


    @RequestMapping("/del")
    public String del(Model model,CustomerInfo customerInfo,@RequestParam("current") Integer current, @RequestParam("size") Integer size){

//        customerServiceService.removeById(customerService.getCustomerserviceid());
//        IPage<CustomerService> page=new Page<>(current,size);
//        IPage<CustomerService> page1 = customerServiceService.page(page);
//        List<CustomerService> list = page1.getRecords();
//        model.addAttribute("list",list);

        this.customerInfoService.removeById(customerInfo.getId());
        initcustomerInfoService(model,current,size);

        return "customerinfo";
    }
    @RequestMapping("updcom")//修改完成
    public String updcom(CustomerInfo customerInfo, Model model, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
        System.out.println("能成功进入");
        CustomerInfo cs = customerInfoService.getById(customerInfo);
        cs.setCustomername(customerInfo.getCustomername());
        cs.setRealname(customerInfo.getRealname());
        cs.setPhone(customerInfo.getPhone());
        cs.setLevel(customerInfo.getLevel());
        cs.setChannel(customerInfo.getChannel());
        cs.setEmail(customerInfo.getEmail());
        customerInfoService.updateById(cs);
        initcustomerInfoService(model,current,size);
        return "customerinfo";
    }
    @RequestMapping("create")//创建订单界面
    public String create(Model model){

        return "createcustomerinfo";
    }
    @RequestMapping("createcom")//点击创建成功
    public String createcom(CustomerInfo customerInfo, Model model){
        LocalDateTime localDateTime = LocalDateTime.now();
        model.addAttribute("localDateTime",localDateTime);
        customerInfo.setCreatetime(localDateTime);
        customerInfoService.save(customerInfo);
        return "createcustomerinfo";
    }

}

